// pages/order/list/index.js
import {
  request,
  getLocation
} from '../../utils/requestUtils';
Page({
  // 页面的初始数据
  data: {
    refreshTriggered: false,
    total: 0,
    page: {
      pageNum: 1,
      pageSize: 10,
    },
    params: {
      keywords: '',
      latitude: '',
      longitude: '',
      type: '',
    },
    stallList: []
  },
  onShow() {
    this.getTabBar().setData({
      active: 1
    })
    let stallType = wx.getStorageSync('stallType');
    if (stallType) {
      this.setData({
        'params.type': stallType.dictValue
      })
      let component = this.selectComponent('#stallType')
      if (component) {
        component.updateNode(stallType);
      }
      wx.setStorageSync('stallType', "");
    }
     this.getLocationHander()
    // this.onScrollRefresh()
  },
  //类型选择回调
  dictConResult(e) {
    console.log('e', e.detail)
    this.setData({
      'params.type': e.detail
    })
    this.onScrollRefresh()
  },
  onLoad() {
   // this.onScrollRefresh()
  },
  // 触底刷新
  getNext() {
    console.log('触底')
    if (this.data.page.pageNum * this.data.page.pageSize >= this.data.total) {
      return
    } else {
      this.data.page.pageNum++;
      this.getList()
    }
  },
  onSearch(event) {
    this.setData({
      'params.keywords': event.detail,
    })
    this.onScrollRefresh()
  },
  // 下拉刷新
  onScrollRefresh: function () {
    this.setData({
      'page.pageNum': 1,
      stallList: []
    })
    this.getList()

  },
  // 查询商商家信息
  async getList() {
    const result = await request({
      url: `/merchant/bus/customer/stall/list?pageNum=${this.data.page.pageNum}&pageSize=${this.data.page.pageSize}`,
      method: "POST",
      data: {
        name: this.data.params.keywords,
        params: {
          keywords: this.data.params.keywords,
          latitude: this.data.params.latitude,
          longitude:this.data.params.longitude
        },
        type: this.data.params.type
      }
      // data: {
      //   "params": {
      //     "keywords": this.data.params.keywords,
      //     "latitude": 30.512159,
      //     "longitude": 104.042802
      //   },
      //   type: this.data.params.type
      // }
    })
    this.setData({
      refreshTriggered: false
    })
    if (result.data.code == 200) {
      var stallList = this.data.stallList.concat(result.data.rows)
      console.log('stallList', stallList)
      this.setData({
        stallList: stallList,
        total: result.data.total
      })
    }
  },
  //获取当前位置
  async getLocationHander() {
    const result = await getLocation()
    this.setData({
      'params.latitude': result.latitude,
      'params.longitude': result.longitude
    })
    console.log('params', this.data.params)
    this.onScrollRefresh()
  },
})
